package mdm.led.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;
import mdm.led.entity.SysLog;
import mdm.led.log.OperationLog;
import mdm.led.service.ILogService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "log")
@Api(description = "日志模块")
public class LogController {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogController.class);

    private final ILogService iLogService;

    @Autowired
    public LogController(ILogService iLogService) {
        this.iLogService = iLogService;
    }

    @ApiOperation(value = "日志查询", notes = "请求参数:<br>" +
            "pageIndex 页码<br>" +
            "limit 每页显示条数<br>" +
            "可选查询条件:<br>" +
            "startDate 开始时间<br>" +
            "endDate 结束时间")
    @OperationLog(operationModule = "日志模块", operationFunction = "日志查询")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public AjaxResponse page(@RequestBody SysLog sysLog, @RequestParam Integer pageIndex, @RequestParam Integer limit) {
        try {
            LOGGER.info("------------param:{}---{}---{}", sysLog, pageIndex, limit);
            PageHelper.startPage(pageIndex, limit);
            List<SysLog> list = iLogService.selectByCondition(sysLog);
            PageInfo result = new PageInfo<>(list);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, result);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }

}
